package com.dqlp.yf.rabbitmq.receive;

import com.dqlp.yf.rabbitmq.bean.Order;
import com.dqlp.yf.rabbitmq.constant.MqConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @program: rabbitmq
 * @description:
 * @author: andy
 * @create: 2019-09-09 11:24
 */
@Component
@Slf4j
public class FanoutCustomer {
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstant.FANOUT_EXCHANGE, type = ExchangeTypes.FANOUT, durable = "true"),
            value = @Queue(value = MqConstant.FANOUT_QUEUE, durable = "true")))
    public void fanoutCustomer(@Payload Order order, @Headers Map<String, Object> map, Channel channel) throws IOException {
        System.out.println(order.getId());
        Long delivery = (Long) map.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(delivery, false);
        } catch (IOException e) {
            channel.basicNack(
                    delivery,false,false
            );
        }
    }
}
